﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.Xpo;
using DevExpress.XtraReports.UI;
using MetadataProcessor;

namespace Contacts.Reports
{
    public partial class GenericReport : XtraReport, IReport
    {
        public GenericReport(ReportTemplateMetadata meta)
            : this()
        {
            this.Tag = meta;
        }

        public GenericReport()
        {
            InitializeComponent();
        }

        public void LoadDataSource()
        {
            if(Tag == null) return;
            ReportTemplateMetadata metadata = Tag as ReportTemplateMetadata;
            dynamic t = MetadataController.GetInstance(metadata.EntityName);
            var collection = new XPCollection(t.GetType());
            //List<object> dataList = ClientData.GetData(@"context.Developers.ToList<object>();", "ReportDTO.Customer");
            List<object> dataList = ClientData.GetData(metadata.Query, metadata.ReturnType);
            foreach (var o in dataList)
            {
                collection.Add(o);
            }
            collection.Add(t);
            var xpCollection = collection;
            DataSource = xpCollection;
        }
    }
}
